<template>
	<view>
		<view class="d-flex j-sb a-center">
			<view class="font-lg font-weight">用户管理</view>
			
			<view class="rounded bg-primary button font-md text-white d-flex a-center j-center"  @click="onAdd">新增</view>
		</view>
	
		<uni-table class="mt-1" style="height: 280rpx"  ref="table" border stripe emptyText="暂无更多数据">
			<uni-tr class="position-sticky bg-white" >
				<uni-th><text class="font-md">手机号</text></uni-th>
				<uni-th><text class="font-md">姓名</text></uni-th>
				<uni-th ><text class="font-md">部门</text></uni-th>
				<uni-th ><text class="font-md">物品类型</text></uni-th>
				<uni-th ><text class="font-md">物品</text></uni-th>
				<uni-th ><text class="font-md">数量</text></uni-th>
				<uni-th ><text class="font-md">类型</text></uni-th>
				<uni-th ><text class="font-md">时间</text></uni-th>
				<uni-th width="80"><text class="font-md">操作</text></uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in tableData" :key="index">
				<uni-td>
					<text class="font-md" >{{item.phone}}</text>
				</uni-td>
				<uni-td>
					<text class="font-md" v-html="item.name"></text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.depname}}</text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.gcategory}}</text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.gname}}</text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.number}}</text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.type}}</text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.datatime}}</text>
				</uni-td>
				
				<uni-td>
					
					<view class="d-flex a-center">
						<view @click="onView(item)" class="font-md text-primary">查看</view>
					</view>
					
				</uni-td>
			</uni-tr>
		</uni-table>
		
	
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { useStore } from '@/store/index.js';
const store = useStore();

const tableData = ref([])
const dataPickerTree = ref([]);
const popupRef= ref(null);
const popupTitle = ref('');

const formData = ref({});
const formRef = ref(null);
const rules = ref({
	name: {
		rules: [{
			required: true,
			errorMessage: '请输入姓名',
		}],
	},
	phone: {
		rules: [{
			required: true,
			errorMessage: '请输入手机号',
		},
		// {
		// 		minLength: 11,
		// 		maxLength: 11,
		// 		errorMessage: '请正确输入11位数字的手机号',
		// },
		],
	},
	did: {
		rules: [{
			required: true,
			errorMessage: '请选择部门',
		}],
	},
	status: {
		rules: [{
			required: true,
			errorMessage: '请选择状态',
		}],
	},
});



const getDepName = (id) => {
	return store.depStore.getNameStrand(id)
}


const reLoad = () => {
	tableData.value = store.userStore.list;
	dataPickerTree.value = store.depStore.getDataPickerTree()
	formData.value = {id:'',name:'',username:'',did:'',status:1,rfid:''};
};


const onAdd = (type) => {
	popupTitle.value = '新增用户';
	popupRef.value.open();
};

const onEdit = (item) => {
	popupTitle.value = '编辑用户';
	formData.value = item;
	popupRef.value.open();
};
const onDel = (item) => {
	store.userStore.del(item.id)
	reLoad()
};


const onsave = () => {
	uni.showLoading()
	formRef.value.validate().then((res) => {
			uni.hideLoading();
			
			const goods  =  store.userStore.list.find(item => item.name == formData.value.name)
			
			if(!goods || formData.value.id == goods.id){
				popupRef.value.close();
				store.userStore.save(formData.value)
				reLoad()
			}else{
				uni.$xwl.toast('用户已存在')
			}

	}).catch((err) => {
			uni.hideLoading();
			console.log('表单错误信息：', err);
	});
	
	
};

onLoad(() => {reLoad()});
</script>

<style lang="scss">
	.button {
		width: 60rpx;
		height: 20rpx;
	}
	.model-shell {
		width: 300rpx;
		border-radius: 6rpx;
		.form-label {
			text-align: right;
			width: 50rpx;
		}
	}
	
	.boxItem{
		width: 71.3rpx;
		height: 46rpx;
		padding: 4rpx;
	}
</style>
